// +----------------------------------------------------------------------
// | BossHongshi [鸿时互娱管理系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------

const fs = require('fs');
module.exports = class extends think.cmswing.admin {
  /**
     * index action
     * @return {Promise} []
     */
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
  }

  /**
     * 玩家首页
     * @returns {*}
     */

  async indexAction() {
	  //page
	
	const data ={
			"count":0,
			"totalPages":0,
			"pageSize":10,
			"currentPage":this.get('page') || 1,
	};
	let players = this.mongoose('mj_players', 'mongoose'); // use `mongoose` adapter type
	let count  = await players.count({}).lean().exec();
	console.log("count -----------:"+count);
	data.count = count;
	data.totalPages =Math.floor((count - 1) / 10 + 1);
	console.log("totalPages -----------:"+data.totalPages);
	//pageCount
	let start = ((this.get('page') || 1)-1)*10;
	let playerdata  = await players.find({}).skip(start).limit(10).sort({'create_time':-1}).lean().exec();
	console.log("playerdata -----------:"+JSON.stringify(playerdata));
	playerdata.forEach(v => {
	      v.last_login_time = v.last_login_time*1000;
	      v.create_time = v.create_time*1000;
	});
	
	data.data =playerdata;
    const html = this.pagination(data);
    console.log("html -----------:"+JSON.stringify(html));
    this.assign('pagerData', html); // 分页展示使用
    this.assign('list', data.data);
    this.meta_title = '玩家列表';
    return this.display();
  }
  
  async statAction() {
	  const map = {
	    		'status': ['>', -1],
	    		is_admin:0
	    	};
	    
	    const auth_role = await this.model('auth_role').alias('ar').join({
		      table: 'auth_user_role',
		      join: 'left',
		      as: 'aur',
		      on: ['aur.role_id', 'ar.id']
	    }).where({'aur.user_id':this.user.uid})
	      .field('ar.id,ar.desc,aur.user_id')
	      .find();
	    if(!think.isEmpty(auth_role)&&auth_role.id!=1){
	    	const userInfo = await this.model('member').find(this.user.uid);
	    	map.area = userInfo.area;
	    }
	    
	    const agent_count = await this.model('member').where(map).count();
	   let new_agent_map = map;
	   let start_time = new Date(GetDateStr(0) + ' ' + '00:00:00').getTime();
	   let end_time = new Date(GetDateStr(0) + ' ' + '23:59:59').getTime();
	   
	   new_agent_map.reg_time = {'>=': start_time, '<=':end_time };
	   console.log("new_agent_map -----------:"+JSON.stringify(new_agent_map));
	   const new_agent_count = await this.model('member').where(new_agent_map).count();
	   
	   let players = this.mongoose('mj_players', 'mongoose'); // use `mongoose` adapter type
	  const player_count  = await players.count({}).lean().exec();
	  const new_player_count  = await players.count({create_time:{$gt:parseInt(start_time/1000),$lte:parseInt(end_time/1000)}}).lean().exec();
	  let three_days_ago_time = new Date(GetDateStr(-3) + ' ' + '00:00:00').getTime();
	  console.log("three_days_ago_time -----------:"+this.moment(three_days_ago_time).format('YYYY-MM-DD HH:mm:ss'));
	  const active_player_count  = await players.count({last_login_time:{$gt:parseInt(three_days_ago_time/1000)}}).lean().exec();
	  
	  let current_time = new Date().getTime();
	  console.log("current_time -----------:"+this.moment(current_time).format('YYYY-MM-DD HH:mm:ss'));
	  const online_player_count  = await players.count({last_login_time:{$gt:parseInt((current_time-600000)/1000)}}).lean().exec();
	  
	  
	  let mjrooms = this.mongoose('mj_rooms', 'mongoose'); // use `mongoose` adapter type
	  const friend_room_count  = await mjrooms.count({club_id:0}).lean().exec();
	  const club_room_count  = await mjrooms.count({club_id:{$ne:0}}).lean().exec();
	  
	  let jinbirooms = this.mongoose('jinbi_rooms', 'mongoose'); // use `mongoose` adapter type
	  const jinbi_room_count  = await jinbirooms.count({}).lean().exec();
	  
	  
	  this.assign('todayIncome', 0.0000);
	  this.assign('incomeTotal', 0.0000);
	  this.assign('rebateTotal', 0.0000);
	  this.assign('agent_count', agent_count);
	  this.assign('new_agent_count', new_agent_count);
	  this.assign('player_count', player_count);
	  this.assign('new_player_count', new_player_count);
	  this.assign('active_player_count', active_player_count);
	  this.assign('online_player_count', online_player_count);
	  
	  this.assign('online_round_count',Math.floor(Math.random()*1000));
	  
	  
	  this.assign('friend_room_count', friend_room_count);
	  this.assign('club_room_count', club_room_count);
	  this.assign('jinbi_room_count', jinbi_room_count);
	  this.meta_title = '统计信息';
	  return this.display();
  }

};
